﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SDT.FileSystem;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using SDT.Libraries.Utilities;

namespace SDT.CrawlSystem.Servers.Save
{
    public sealed class SaveFiles
    {
        #region variables
        private const string XML_RESULT_TEMPLATE ="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<_{0}>\r\n\t<TF><![CDATA[{1}]]></TF>\r\n\t <LF><![CDATA[{2}]]></LF>\r\n\t<CF><![CDATA[{3}]]></CF>\r\n</_{4}>";

        private static readonly object syncObj = new object();
        public string htmlFolder
        {
            get;
            set;
        }
        public int id
        {
            get;
            set;
        }
        public int cid
        {
            get;
            set;
        }
        public int pid
        {
            get;
            set;
        }
        public int uid
        {
            get;
            set;
        }
        public string html
        {
            get;
            set;
        }
        public string hf
        {
            get;
            set;
        }
        public string txt
        {
            get;
            set;
        }
        public DateTime df
        {
            get;
            set;
        }
        public string lf
        {
            get;
            set;
        }
        public string tf
        {
            get;
            set;
        }
        public string cf
        {
            get;
            set;
        }
        public Encoding enc
        {
            get;
            set;
        }
        #endregion

        #region constructor
        public SaveFiles(string htmlFolder, int id, int cid, int pid, int uid,string html, string hf, string txt, string tf, DateTime df, string lf, string cf, Encoding enc)
        {
            this.htmlFolder = htmlFolder;
            this.id = id;
            this.cid = cid;
            this.pid = pid;
            this.uid = uid;
            this.html = html;
            this.hf = hf;
            this.tf = tf;
            this.txt = txt;
            this.df = df;
            this.lf = lf;
            this.cf = cf;
            this.enc = enc;

        }
        #endregion
        public void SaveFileTask()
        {
            try
            {


                #region Create directory structure on file-server
                string absPath = string.Format(@"{0}\{1}\{2}\{3}", htmlFolder, cid.ToString(), pid.ToString(), DateTime.Now.ToString("yyMMdd"));
                if (Directory.Exists(absPath))
                {
                    lock (this)
                        absPath = String.Format("{0}{1}{2}", absPath, Path.DirectorySeparatorChar, uid);
                }
                else
                {
                    lock (this)
                    {
                        DirectoryInfo di = Directory.CreateDirectory(absPath);
                        absPath = String.Format("{0}{1}{2}", absPath, Path.DirectorySeparatorChar, uid);
                    }
                }
                #endregion
                   #region Save xml result
                try
                {
                    if (!File.Exists(absPath))
                    {
                        lock (syncObj)
                        {
                            using (TextWriter tw = new StreamWriter(String.Format("{0}.xml", absPath)))
                            {
                                tw.Write(string.Format(XML_RESULT_TEMPLATE, id, string.IsNullOrEmpty(this.tf) ? "" : tf.Trim(), string.IsNullOrEmpty(lf) ? "" : lf.Trim(), string.IsNullOrEmpty(cf) ? "" : cf.Trim(), id));
                                tw.Close();
                            }
                        }
                    }
                    else
                    {
                        new LogException().WriteSaveFile(string.Format("  Module {0} --Msg {1} --{2}", "Error Save File XML", "loi ko xac dinh", absPath));
                    }
                }
                catch (Exception ex)
                {
                    new LogException().WriteSaveFile(string.Format("  Module {0} --Msg {1} --{2}", "Error Save File XML", ex.StackTrace, absPath));
                }
              
                #endregion
            

            }
            catch (Exception ex)
            {
                new LogException().WriteSaveFile(string.Format("  Module {0} --Msg {1}--{2}-channelID:{3}-pageid:{4}", "Error Save File webpage 1", ex.ToString(), this.htmlFolder, this.cid, this.uid));
            }

        }
    }
}
